﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace KTX.Vinh
{
    public partial class frmRoomStatus : Form
    {
        #region thuoctinh
        private SqlConnection conn;
        int dong = 0;
        string str = "";
        #endregion
        public frmRoomStatus()
        {
            InitializeComponent();
            Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var strConnectionString = _config.ConnectionStrings.ConnectionStrings[1].ConnectionString;
            conn = new SqlConnection(strConnectionString);
            kichHoatButton(false);
            createColDtg();
        }
        #region phuong thuc

        public void kichHoatButton(bool a)
        {
            btThem.Enabled = a;
            btSua.Enabled = !a;
            btXoa.Enabled = !a;
            btnHuy.Enabled = a;
        }
        public void createColDtg(){
            // tạo cột ID
            dtgRoomStatus.Columns.Clear();
            DataGridViewTextBoxColumn idcol = new DataGridViewTextBoxColumn();
            idcol.Name = "Id";
            idcol.HeaderText = "Mã trạng thái phòng";
            idcol.DataPropertyName = "Id";
            idcol.Width = 150;
            idcol.ReadOnly = true;
            // tạo cột name
            DataGridViewTextBoxColumn namecol = new DataGridViewTextBoxColumn();
            namecol.Name = "Name";
            namecol.HeaderText = "Tên trạng thái phòng";
            namecol.DataPropertyName = "Name";
            namecol.Width = 150;
            dtgRoomStatus.Columns.AddRange(new DataGridViewColumn[] { idcol, namecol });
            dtgRoomStatus.Columns["Id"].DefaultCellStyle.ForeColor = Color.Red;
            dtgRoomStatus.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGoldenrodYellow;
            dtgRoomStatus.RowsDefaultCellStyle.BackColor = Color.AliceBlue;
        }
        #endregion

        #region sự kiện


        private void frmRoomStatus_Load(object sender, EventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

                var dapt = new SqlDataAdapter("select * from tb_Status",conn);
                var ta = new DataTable();
                dapt.Fill(ta);
                bindingSource1.DataSource = ta;
                dtgRoomStatus.DataSource = bindingSource1;
         
            }
            catch (Exception ex)
            {
            	MessageBox.Show(ex.Message);
            }
           
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
                kichHoatButton(false);
                dong = dtgRoomStatus.CurrentCell.RowIndex;
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

                if(dtgRoomStatus.Rows[dong].Cells[1].Value.ToString().Equals(""))
                    MessageBox.Show("Vui lòng nhập thông tin trạng thái phòng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else {
                var cmd = new SqlCommand("insert into tb_Status values(@name)",conn);
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 100).Value = dtgRoomStatus.Rows[dong].Cells[1].Value.ToString();
                cmd.ExecuteNonQuery();
                }
                frmRoomStatus_Load(sender,e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
          
        private void btXoa_Click(object sender, EventArgs e)
        {

            try
            {
                dong = dtgRoomStatus.CurrentCell.RowIndex;
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

                var cmd = new SqlCommand("delete from tb_Status where Id = @Id", conn);
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Convert.ToInt32(dtgRoomStatus.Rows[dong].Cells[0].Value);
                if (MessageBox.Show("Ban co muon xoa du lieu nay khong?", "Thong bao", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    cmd.ExecuteNonQuery();
                
                frmRoomStatus_Load(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                dong = dtgRoomStatus.CurrentCell.RowIndex;
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

                var cmd = new SqlCommand("update tb_Status set Name = @name where Id = @id", conn);
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Convert.ToInt32(dtgRoomStatus.Rows[dong].Cells[0].Value);
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 100).Value = dtgRoomStatus.Rows[dong].Cells[1].Value.ToString();
                cmd.ExecuteNonQuery();
                frmRoomStatus_Load(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            kichHoatButton(true);
        }
        

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmRoomStatus_Load(sender, e);
            kichHoatButton(false);
        }
       

        private void rdName_CheckedChanged(object sender, EventArgs e)
        {
           
            txtTimKiem.Text = "";
            txtTimKiem.Enabled = true;
            str = "Name";
            txtTimKiem.Focus();
            txtTimKiem.ForeColor = Color.Black;
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                AutoCompleteStringCollection auto1 = new AutoCompleteStringCollection();
                var cmd = new SqlCommand("select * from tb_Status", conn);
                var rd = cmd.ExecuteReader();
                if (rd != null)
                {
                    while (rd.Read())
                    {
                        auto1.Add(rd[1].ToString());
                    }
                }
                txtTimKiem.AutoCompleteMode = AutoCompleteMode.Append;
                txtTimKiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtTimKiem.AutoCompleteCustomSource = auto1;
                txtTimKiem.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtTimKiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtTimKiem.AutoCompleteCustomSource = auto1;
                rd.Close();
                cmd.Dispose();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
              
           
        }

        private void rdID_CheckedChanged(object sender, EventArgs e)
        {
           
            txtTimKiem.Text = "";
            str = "Id";
            txtTimKiem.Enabled = true;
            txtTimKiem.ForeColor = Color.Black;
            txtTimKiem.Focus();
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                AutoCompleteStringCollection auto1 = new AutoCompleteStringCollection();
                var cmd = new SqlCommand("select * from tb_Status", conn);
                var rd = cmd.ExecuteReader();
                if (rd != null)
                {
                    while (rd.Read())
                    {
                        auto1.Add(rd[0].ToString());
                    }
                }
                txtTimKiem.AutoCompleteMode = AutoCompleteMode.Append;
                txtTimKiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtTimKiem.AutoCompleteCustomSource = auto1;
                txtTimKiem.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtTimKiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtTimKiem.AutoCompleteCustomSource = auto1;
                rd.Close();
                cmd.Dispose();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

                if (txtTimKiem.Text.Equals(""))
                    createColDtg();
                if (!txtTimKiem.Text.Equals(""))
                {
                        var strqr = string.Format("select * from tb_Status where {0} like N'%{1}%'", str, txtTimKiem.Text);
                        var dapt = new SqlDataAdapter(strqr, conn);
                        var ta = new DataTable();
                        ta.Clear();
                        dapt.Fill(ta);
                        bindingSource1.DataSource = ta;
                        dtgRoomStatus.DataSource = bindingSource1;
                }
                       
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
